import { BasicColumn, FormSchema } from '@apps/antd-admin/src/components/Table';
import { companyList } from '@apps/ant-bus/src/views/business/companyManage/company/data/api';
import { setCompanyUserStatus } from '../data/api';
import { rules } from '@pkg/utils';
import { h } from 'vue';
import { Switch } from 'ant-design-vue';

export const columns: BasicColumn[] = [
  {
    title: '前端账户',
    dataIndex: 'mainAccount',
    width: 120,
  },
  {
    title: '账号姓名',
    dataIndex: 'userName',
    width: 100,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 120,
    customRender: ({ record, value }: Recordable) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.status === 1,
        checkedChildren: '已启用',
        unCheckedChildren: '已禁用',
        loading: record.pendingStatus,
        onChange() {
          record.pendingStatus = false;
          setCompanyUserStatus({ userId: record.userId })
            .then(data => {
              if (value == 0) {
                record.status = 1;
              }
              if (value == 1) {
                record.status = 0;
              }
            })
            .catch(data => {
              console.info('catch', data);
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 150,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'mainAccount',
    label: '前端账户',
    component: 'Input',
    colProps: { span: 4 },
  },
  {
    field: 'userName',
    label: '账号姓名',
    component: 'Input',
    colProps: { span: 4 },
  },
];

export const formSchema: FormSchema[] = [
  {
    label: '合作企业账号Id',
    field: 'userId',
    component: 'Input',
    required: false,
    show: false,
  },
  {
    label: '合作企业',
    field: 'busCompanyId',
    component: 'ApiSelect',
    required: true,
    dynamicDisabled: true,
    componentProps: {
      api: companyList,
      params: { handlerName: 'baseManageUserCompanyListHandler' },
      labelField: 'companyName',
      valueField: 'companyId',
    },
  },
  {
    label: '前端账户',
    field: 'mainAccount',
    component: 'Input',
    dynamicDisabled: ({ values }) => {
      if (values.employeeId) {
        return true;
      } else {
        return false;
      }
    },
    dynamicRules: ({ model }) => {
      return [
        {
          required: true,
          validator: async (_, value) => {
            console.info(model);
            if (model.employeeId) {
              return Promise.resolve();
            }
            if (!value) {
              return Promise.reject('请输入员工账户');
            }
            /*  let isExist = await isExistByAccount(value);
          if (isExist) {
            return Promise.reject('该员工账户已存在');
          }*/
            return Promise.resolve();
          },
          trigger: 'change',
        },
      ];
    },
  },
  {
    label: '账号姓名',
    field: 'userName',
    component: 'Input',
    required: true,
  },
  {
    label: '手机号',
    field: 'mobile',
    component: 'Input',
  },
  {
    label: '密码',
    field: 'password',
    component: 'Input',
    ifShow: ({ values }) => !values.userId,
  },
  {
    label: '确认密码',
    field: 'confirmPassword',
    component: 'InputPassword',
    dynamicRules: ({ values }) => rules.confirmPassword(values, true),
    ifShow: ({ values }) => !values.userId,
  },
];
